/*



 */
package com.bbh.plugin.single.privilege.demo.controller;


import com.bbh.plugin.single.privilege.core.entity.App;
import com.bbh.plugin.single.privilege.core.enums.BaseEnum;
import com.bbh.plugin.single.privilege.core.service.AccountAppService;
import com.bbh.plugin.single.privilege.core.service.AppService;
import com.bbh.plugin.single.privilege.core.vo.AppVo;
import com.bbh.common.base.vo.GridVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.domain.Message;
import com.bbh.common.domain.Page;
import com.bbh.common.domain.StatusType;
import com.bbh.common.utils.IdUtils64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.config.shiro.base.RuleUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;

/**
 * Controller - 菜单
 *
 * @author KCSHOP Team
 * @version 3.0
 */
@Controller
@RequestMapping("/app")
public class AppController extends BasicController {
    @Autowired
    private AppService appService;
    @Autowired
    private AccountAppService accountAppService;
    /**
     * 跳转菜单页面
     *
     * @author bestgwd
     */
    @RequestMapping(value = VIEW_FILED + "/index")
    public String index(ModelMap model) {
        model.addAttribute("statusItems", EnumSet.allOf(BaseEnum.IS_ENABLED.class));
        return "single/app/index";
    }

    @RequestMapping(value = INSERT_FILED + "/insert")
    public
    @ResponseBody
    Message insert(@Valid App app) {
        Date date = new Date();
        app.setCreateTime(date);
        app.setModifyTime(date);
        app.setCreateBy(getPrincipal().getLoginName());
        app.setModifyBy(getPrincipal().getLoginName());
        app.setDataStatus(BaseEnum.DATA_STATUS.NORMAL.getKey());
        app.setId(IdUtils64.getInstance().getStringId());
        app.setAppKey(StringUtils.upperCase(app.getAppKey()));
        appService.insert(app);
        return new Message(StatusType.SUCCESS.getKey(), "保存成功");
    }

    /**
     * @param request
     * @param model
     * @param appName
     * @param isEnabled
     * @param appNameCode
     * @param exclude     排除系统
     * @return
     * @throws ServletRequestBindingException
     */
    @RequestMapping(value = {VIEW_FILED + "/list", "list/" + NORMAL_FILED})
    public
    @ResponseBody
    GridVo list(HttpServletRequest request, Model model, String appName, String isEnabled, String appNameCode, String exclude) throws ServletRequestBindingException {
        Condition condition = new Condition();
        condition.putEquals("dataStatus", BaseEnum.DATA_STATUS.NORMAL.getKey());
        condition.putNotBlank("appName", Condition.LIKE, appName);
        condition.putNotBlank("appNameCode", Condition.LIKE, appNameCode);
        condition.putEquals("isEnabled", isEnabled);
        if (RuleUtils.hasRule(RuleUtils.LEVEL0)) {//限制只有当前账号的数据
            condition.put("appNameCode", Condition.IN, accountAppService.findAccountAppCodeList(getLoginId()));
        }
        condition.putNotBlank("appNameCode", Condition.NE, exclude);
        GridVo vo = new GridVo();
        Page<App> pageResult = appService.selectPage(condition, null, initPageRequest(request, PAGE_NUMBER, PAGE_ROWS));

        return new GridVo(pageResult);
    }

    /**
     * 跳转到edit页面
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED + "/edit")
    public String edit(String id, Model model, String type) {
        App app;
        if ("edit".equals(type)) {
            app = appService.selectByPrimaryKey(id);
            model.addAttribute("app", app);
            model.addAttribute(OPERATE_TYPE, "edit");
        }
        if ("add".equals(type)) {
            model.addAttribute(OPERATE_TYPE, "add");
        }
        return "single/app/edit";
    }

    /**
     * update
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED + "/update")
    public
    @ResponseBody
    Message update(@Valid App app) {
        Assert.notNull(app.getId());
        App updateApp = appService.selectByPrimaryKey(app.getId());
        if (updateApp != null) {
            updateApp.setAppName(app.getAppName());
            updateApp.setIsEnabled(app.getIsEnabled());
            updateApp.setDescription(app.getDescription());
            updateApp.setModifyBy(getPrincipal().getLoginName());
            updateApp.setModifyTime(new Date());
            updateApp.setAppKey(StringUtils.upperCase(app.getAppKey()));
            appService.updateByPrimaryKeySelective(updateApp);
        }
        return new Message(StatusType.SUCCESS.getKey(), "更新成功");
    }

    @RequestMapping(DELETE_FILED + "/delete")
    public
    @ResponseBody
    Message delete(@RequestParam(value = "ids[]") String[] ids) {
        for (String id : ids) {
            App app = appService.selectByPrimaryKey(id);
            if (app != null) {
                app.setDataStatus(BaseEnum.DATA_STATUS.DELETE.getKey());//设置为不可用
                app.setModifyBy(getPrincipal().getLoginName());
                app.setModifyTime(new Date());
                appService.updateByPrimaryKeySelective(app);
            }
        }
        Message message = new Message(StatusType.SUCCESS.getKey(), "删除成功");
        return message;
    }

    /**
     * 跳转到应用选择界面
     *
     * @param model
     * @param idField
     * @param textField
     * @param exclude   排除的应用
     * @return
     */
    @RequestMapping(value = {VIEW_FILED + "/appList", "/toList/" + NORMAL_FILED}, method = RequestMethod.GET)
    public String appList(ModelMap model, String idField, String textField, String exclude) {
        return "app/appList";
    }

    @RequestMapping(value = VIEW_FILED + "/accountAppCodeList")
    public
    @ResponseBody
    List<AppVo> accountAppCodeList(HttpServletRequest request, String appName, String appNameCode, String accountId) throws ServletRequestBindingException {
        Condition condition = new Condition();
        condition.putEquals("dataStatus", BaseEnum.DATA_STATUS.NORMAL.getKey());
        if (StringUtils.isNoneBlank(accountId)) {
            condition.put("accountId", accountId);
        }

        List<AppVo> pageResult = accountAppService.findAccountAppCodeList(condition);

        return pageResult;
    }


}